package com.augurit.gzcgwsp.sendNotice.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.syslog.tool.SysTraceLogWriter;
import com.augurit.gzcgwsp.sendNotice.entity.YwWebserviceInfo;
import com.augurit.gzcgwsp.sendNotice.foresee.gzcgw.webservice.util.WSInvoker;
import com.augurit.gzcgwsp.sendNotice.service.ICallWebServiceImpl;
import com.augurit.gzcgwsp.sendNotice.service.ICreateNoticeCommonPartService;
import com.augurit.gzcgwsp.sendNotice.service.IYwWebserviceInfoService;
import com.foresee.spiderweb.console.security.bean.ServiceResult;

@Service
@Transactional
public class CallWebServiceImpl implements ICallWebServiceImpl {
	Logger logger = (Logger) LoggerFactory.getLogger(CallWebServiceImpl.class);
	@Autowired
	private ICreateNoticeCommonPartService createNoticeCommonPartService;
	@Autowired
	private IYwWebserviceInfoService ywWebserviceInfoService;
	
	/**
	 * 调用webservice接口  案件状态接口
	 * 
	 * @param form
	 * @param task
	 */
	public void callWebService2() {
//	public void callWebService2(Map<String,String> noticeLabelMap) {
		WSInvoker wsInvoker = new WSInvoker();
		List list= createNoticeCommonPartService.getWebServicePara();
		for(int i = 0;i<list.size();i++){
			List paraList = (List)list.get(i);
			String wsdl = (String)paraList.get(0);
			String SPXT_ApprovalInfoFeedBack_Service = (String)paraList.get(1);
			String feedBackApprovalInfo = (String)paraList.get(2);
			String para = (String)paraList.get(3);
			long id = (Long)paraList.get(4);
			String acceptNo = (String)paraList.get(5);
			String approvalNo = (String)paraList.get(6);
			Object[] o = new Object[3];
			o[0] =  para;
			o[1] = acceptNo;
			o[2] = approvalNo;
			try {
				ServiceResult serviceResult = null;
				serviceResult = wsInvoker.invokeWS(wsdl,SPXT_ApprovalInfoFeedBack_Service,feedBackApprovalInfo, o);
				System.out.println("serviceResult = "+serviceResult);
				System.out.println("return = "+serviceResult.getRetCode());
				System.out.println("serviceResult.getRetMessage() = "+serviceResult.getRetMessage());
				
				if (serviceResult != null) {
					String flag = serviceResult.getRetCode(); //flag等于0，成功，flag等于1失败   成功就把状态改了
					if("0".equals(flag)){
						ywWebserviceInfoService.changState(id);
					}else if("1".equals(flag)){//失败，把交换次数保存到数据库，超过50次，则第二天再交换
						ywWebserviceInfoService.callRecord(id);
					}
				} else {
					ywWebserviceInfoService.callRecord(id);
					System.out.println("WS invoked failed");
				}
			} catch (Exception e) {
				ywWebserviceInfoService.callRecord(id);
				logger.error("(接口)调用方欣数据交换接口异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
				e.printStackTrace();
			}
		}	
	}
	
	
	/**
	 * 调用webservice接口  案件状态接口
	 * 
	 * @param form
	 * @param task
	 */
	public void callStopPublicWebService() {
		WSInvoker wsInvoker = new WSInvoker();
		List resultList = ywWebserviceInfoService.getStopPublicYwWebserviceInfo();
		System.out.println("resultList = "+resultList.size());
		boolean dateEqualFlag = false;
		for(int i = 0;i<resultList.size();i++){
			YwWebserviceInfo ywWebserviceInfo = (YwWebserviceInfo)resultList.get(i);
			if(ywWebserviceInfo.getEndDate()!=null){//如果结束时间不为空，则检查是否是今天；如果是，则交换，否则，不交换
				Date endDate = ywWebserviceInfo.getEndDate();
				SimpleDateFormat myFmt=new SimpleDateFormat("yyMMdd");		
				String endDateStr = myFmt.format(endDate);//结束日期 6位
				String todayStr = myFmt.format(new Date());//今天日期 6位
//				System.out.println("endDateStr----------->>"+endDateStr);
//				System.out.println("todayStr----------->>"+todayStr);
//				if(todayStr.equals(endDateStr)){//两个日期相等
//					dateEqualFlag = true;
//				} 
				if(Integer.parseInt(endDateStr) <= Integer.parseInt(todayStr)){//结束日期小于或者等于今天日期
					dateEqualFlag = true;
				}
			}
			
			if(ywWebserviceInfo.getEndDate()==null || dateEqualFlag == true){
				//调用wsdl地址
				String wsdl = ywWebserviceInfo.getWsdlPara();
				//调用服务名
				String SPXT_ApprovalInfoFeedBack_Service  = ywWebserviceInfo.getParaTwo();
				//调用方法：待办获取方法
				String feedBackApprovalInfo  = ywWebserviceInfo.getParaThree();
//				String stopPublicApprovalInfoXML = ywWebserviceInfo.getFilePath();
				String xmlStr = ywWebserviceInfo.getXmlStr();
				long id = ywWebserviceInfo.getId();
				try {
					ServiceResult serviceResult = null;
					serviceResult = wsInvoker.invokeWS(wsdl,
							SPXT_ApprovalInfoFeedBack_Service,
							feedBackApprovalInfo, new Object[] {xmlStr});
					if (serviceResult != null) {
						String flag = serviceResult.getRetCode(); //flag等于0，成功，flag等于1失败   成功就把状态改了
						if("0".equals(flag)){
							ywWebserviceInfoService.changState(id);
						}else if("1".equals(flag)){//失败，把交换次数保存到数据库，超过50次，则第二天再交换
							ywWebserviceInfoService.callRecord(id);
						}
					} else {
						ywWebserviceInfoService.callRecord(id);
						System.out.println("WS invoked failed");
					}
				} catch (Exception e) {
					ywWebserviceInfoService.callRecord(id);
					logger.error("(接口)调用方欣结束案件公示接口异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
					e.printStackTrace();
				}			
			}
			
		}		
	}
	
	/**
	 * 交换还没有发送的同步用户或者行政组织记录
	 * @return
	 */
	public void syncUserAndOrgWebService() {
		WSInvoker wsInvoker = new WSInvoker();
		List list= ywWebserviceInfoService.getsyncUserAndOrgYwWebServiceInfo();
		for(int i = 0;i<list.size();i++){
			YwWebserviceInfo ywWebserviceInfo = (YwWebserviceInfo)list.get(i);
			String wsdl = ywWebserviceInfo.getWsdlPara();
			//调用服务名
			String SPXT_ApprovalInfoFeedBack_Service  = ywWebserviceInfo.getParaTwo();
			//调用方法：待办获取方法
			String feedBackApprovalInfo  = ywWebserviceInfo.getParaThree();
//			String xmlStr = ywWebserviceInfo.getFilePath();
			String xmlStr = ywWebserviceInfo.getXmlStr();
			long id = ywWebserviceInfo.getId();
			try {
				ServiceResult serviceResult = null;
				serviceResult = wsInvoker.invokeWS(wsdl,SPXT_ApprovalInfoFeedBack_Service,feedBackApprovalInfo, new Object[] {xmlStr});
				System.out.println("serviceResult = "+serviceResult);
				System.out.println("return = "+serviceResult.getRetCode());
				System.out.println("serviceResult.getRetMessage() = "+serviceResult.getRetMessage());				
				if (serviceResult != null) {
					String flag = serviceResult.getRetCode(); //flag等于0，成功，flag等于1失败   成功就把状态改了
					if("0".equals(flag)){
						ywWebserviceInfoService.changState(id);
					}else if("1".equals(flag)){//失败，把交换次数保存到数据库，超过50次，则第二天再交换
						ywWebserviceInfoService.callRecord(id);
					}
				} else {
					ywWebserviceInfoService.callRecord(id);
					System.out.println("WS invoked failed");
				}
			} catch (Exception e) {
				ywWebserviceInfoService.callRecord(id);
				logger.error("(接口)同步用户或者行政组织到方欣的："+feedBackApprovalInfo+" 接口异常 "+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
				e.printStackTrace();
			}
		}	
	}
}
